package com.hx.hospital.mapper;

import com.hx.hospital.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;
import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper<User> {

    @Results(id = "openIdResult",
            value={@Result(property = "id", column = "id", jdbcType = JdbcType.BIGINT),
                    @Result(property = "username", column = "username", jdbcType = JdbcType.VARCHAR),
                    @Result(property = "photo", column = "photo", jdbcType = JdbcType.VARCHAR),
                    @Result(property = "address", column = "address", jdbcType = JdbcType.VARCHAR),
                    @Result(property = "sex", column = "sex", jdbcType = JdbcType.INTEGER),
                    @Result(property = "phone", column = "phone", jdbcType = JdbcType.VARCHAR),
                    @Result(property = "created", column = "created", jdbcType = JdbcType.DATETIMEOFFSET),
                    @Result(property = "lastLoginTime", column = "last_login_time", jdbcType = JdbcType.DATETIMEOFFSET),
                    @Result(property = "token", column = "token", jdbcType = JdbcType.VARCHAR),
                    @Result(property = "openId", column = "open_id", jdbcType = JdbcType.VARCHAR)})
    @Select("select id,username,photo,address,sex,phone,created,last_login_time,token,open_id from user where open_id=#{openId}")
    public User findUserByOpenId(String openId);

    @Results(id = "phoneResult",
            value={@Result(property = "id", column = "id", jdbcType = JdbcType.BIGINT),
                    @Result(property = "username", column = "username", jdbcType = JdbcType.VARCHAR),
                    @Result(property = "photo", column = "photo", jdbcType = JdbcType.VARCHAR),
                    @Result(property = "address", column = "address", jdbcType = JdbcType.VARCHAR),
                    @Result(property = "sex", column = "sex", jdbcType = JdbcType.INTEGER),
                    @Result(property = "phone", column = "phone", jdbcType = JdbcType.VARCHAR),
                    @Result(property = "created", column = "created", jdbcType = JdbcType.DATETIMEOFFSET),
                    @Result(property = "lastLoginTime", column = "last_login_time", jdbcType = JdbcType.DATETIMEOFFSET),
                    @Result(property = "token", column = "token", jdbcType = JdbcType.VARCHAR),
                    @Result(property = "openId", column = "open_Id", jdbcType = JdbcType.VARCHAR)})
    @Select("select id,username,photo,address,sex,phone,created,last_login_time,token,open_Id from user where phone=#{phone}")
    public User findUserByPhone(String phone);


    @Insert("insert into user(id,username,photo,address,sex,phone,created,last_login_time,token,open_Id)" +
            "values(#{id},#{username},#{photo},#{address},#{sex},#{phone},#{created},#{last_login_time},#{token},#{open_Id})")
    public void add(User user);
}
